package org.erp.common.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.erp.common.constant.Constants;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author libin
 * @version 1.0
 * @description: 响应信息主体
 * @date 2025/11/5 10:01
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;
    /**
     * 失败
     */
    public static final int FAIL = Constants.FAIL;

    private static final long serialVersionUID = 1L;
    private int code;

    private String msg;

    private T data;

    public static <T> Result<T> success() {
        return restResult(null, SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return restResult(data, SUCCESS, null);
    }

    public static <T> Result<T> success(String msg) {
        return restResult(null, SUCCESS, msg);
    }

    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> error() {
        return restResult(null, FAIL, null);
    }

    public static <T> Result<T> error(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> error(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> Result<T> error(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> Result<T> warn(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> Result<T> restResult(T data, int code, String msg) {
        return Result.<T>builder()
                .code(code)
                .data(data)
                .msg(msg)
                .build();
    }

    public Boolean isError() {
        return Result.SUCCESS != this.getCode();
    }

    public Boolean isSuccess() {
        return Result.SUCCESS == this.getCode();
    }
}
